import Vue from 'vue'
import Router from 'vue-router'

import Layout from '@/layout'

import studentsRouter from './modules/students'
import projectRouter from './modules/project'
Vue.use(Router)
// constantRoutes 是静态路由，不需要动态权限判断的路由，如登录页、404、等通用页面；

export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    meta: { title: '后台管理中心登录' },
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  }
]
// constantRoutes 是动态路由，需要动态权限判断才能通过 addRoutes 动态添加的页面；
export const asyncRoutes = [
  {
    path: '/consumer',
    component: Layout,
    redirect: '/consumer/user',
    alwaysShow: true, // 将始终显示在菜单栏
    name: 'Consumer',
    meta: { title: '用户管理', icon: '用户管理' }, // bfcbd9
    children: [
      {
        path: 'user',
        component: () => import('@/views/user'),
        name: 'User',
        meta: { title: '用户列表', icon: 'list' }
      },
      {
        path: 'role',
        component: () => import('@/views/roles'),
        name: 'Role',
        meta: { title: '角色管理', icon: 'role' }
      }
    ]
  },
  studentsRouter,
  projectRouter,
  {
    path: '/curriculum',
    component: Layout,
    redirect: '/curriculum/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/course'),
        name: 'Curriculum',
        meta: { title: '课程管理', icon: 'money' }
      }
    ]
  },
  {
    path: '/menu',
    component: Layout,
    redirect: '/menu/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/menu'),
        name: 'Menu',
        meta: { title: '菜单管理', icon: 'example' }
      }
    ]
  },
  {
    path: '/examine',
    component: Layout,
    redirect: '/examine/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/examine'),
        name: 'Examine',
        meta: { title: '千分制审核', icon: 'skill' }
      }
    ]
  },
  {
    path: '/news',
    component: Layout,
    redirect: '/news/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/news'),
        name: 'News',
        meta: { title: '错误日志', icon: 'bug' }
      }
    ]
  },
  {
    path: '/journal',
    component: Layout,
    redirect: '/journal/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/journal'),
        name: 'Journal',
        meta: { title: '消息管理', icon: 'message' }
      }
    ]
  }
]

const originalPush = Router.prototype.push
// 修改原型对象中的push方法
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
const createRouter = () => new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}
export default router
